//userCenter.js
const util = require("../../utils/util.js");
const config = require("../../utils/config.js");

//获取应用实例
var app = getApp()
Page({
  data: {
    // 待完成任务
    toCompleteTasks: "",
    // 已完成任务
    completedTasks: "",
    // 用户名称
    userName: ""
  },
  onLoad: function () {
    this.refreshNewData();
  },
  //待完成任务点击事件
  toBeDoneAction: function () {
    console.log("待完成任务");
  },
  // 已完成任务
  completedTaskAction: function () {
    console.log("已经完成任务");
  },
  // 退出账号
  logoutBtnAction: function () {
    console.log("logout");
    wx.setStorageSync('adminUserViewId', "");
    wx.setStorageSync('token', "");
    app.globalData.adminUserViewId = "";
    app.globalData.token = "";
    if (app.globalData.adminUserViewId === undefined || app.globalData.token === undefined || app.globalData.adminUserViewId === "" || app.globalData.token === "") {
      wx.redirectTo({ url: '/pages/login/login' });
      console.log("退出账号，调取登录页面");
    }
  },
  //刷新数据
  refreshNewData: function () {
    //加载提示框
    util.showLoading();
    var that = this;
    var urlStr = '/api/adminUser/info';
    util.request(urlStr, "", function (res) {
      console.log("data===" + res.data.code);
      util.hideToast();
      var msg = res.data.msg;
      if (res.data.code === 800 || res.data.code === 900) {
        that.$wuxToast.show({
          type: 'text',
          timer: 1000,
          color: '#fff',
          text: '账号未登录，请重新登录。',
          success: () => console.log('文本内容')
        })
        wx.redirectTo({ url: '/pages/login/login' });
      } else {
        var unFinishCount = res.data.data.unFinishCount;
        var finishCount = res.data.data.finishCount;
        var userNameStr = res.data.data.userName;
        that.setData({
          toCompleteTasks: unFinishCount,
          completedTasks: finishCount,
          userName: userNameStr
        });
      }
    })

  }
})